#!/bin/sh
#
# $Header: has/src/crsagents/oraagent/asmproxy.pl /main/7 2010/01/14 08:50:43 cwang Exp $
#
# asmproxy.pl
#
# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      asmproxy.pl - <one-line expansion of the name>
#
#    DESCRIPTION
#      proxy action_script that calls
#      crsctl start/stop/stat resource resource_name -init
#
#    NOTES
#      proxy action_script that calls
#      crsctl start/stop/stat resource resource_name -init
#      if action is start or stop
#         returns 0
#      if action is stat
#         returns 0 if the ora.asm resource STATE is ONLINE
#         returns 1 if the ora.asm resource STATE is OFFLINE
#
#
#    MODIFIED   (MM/DD/YY)
#    cwang       11/17/09 - fix bug 8659070 log location
#    cwang       09/04/09 - XbranchMerge cwang_lrg-4122477 from
#                           st_has_11.2.0.1.0
#    cwang       08/12/09 - fix export for Solaris Sparc64
#    cwang       06/24/09 - bug 8629463 check msg # not text
#    cwang       06/20/09 - bug 8608264 make a shell script
#    cwang       04/13/09 - bug 8430338 use ORA_CRS_HOME
#    cwang       03/23/09 - bug 8341315 hardcode ora.asm
#    cwang       01/18/08 - Creation
#

if [ $# -eq 1 ]
then
  __action="$1"
  export __action
elif [ $# -eq 2 ]
then
  __action="$2"
  export __action
fi
$ORA_CRS_HOME/perl/bin/perl << __EOF__
use strict;

my \$n;
my \$action;
my \$date;
my \$time;

my \$ORA_CRS_HOME = \$ENV{'ORA_CRS_HOME'};
my \$resname      = "ora.asm";
(\$date,\$time)   = &timestamp();

#get node name
my \$HOSTNAME     = \`\$ORA_CRS_HOME/bin/crsctl get hostname\`;
#remove new line
chomp(\$HOSTNAME);
open  FILE, ">>\$ORA_CRS_HOME/log/\$HOSTNAME/agent/crsd/asmproxy.pl.log" or die \$!;

\$action = \$ENV{'__action'};

if ( \$action eq "check") {
  \$action = "stat";
}

(\$date,\$time) = &timestamp();
if (\$action ne "start" && \$action ne "stop" && \$action ne "stat"){
 print FILE "\$date \$time unknown command \$action\n";
 close FILE;
 exit -1;
}

my \$cmd;
print FILE  "\$date \$time command \$action begin\n" ;
\$cmd = "\$ORA_CRS_HOME/bin/crsctl \$action resource \$resname -init";

open (CRSTEST, "\$cmd |");
my \$rc=1;
while (<CRSTEST>)
{
 (\$date,\$time) = &timestamp();
 print FILE "\$date \$time \$_" if \$_ =~ /^\S/;
 if (\$action eq "start")
 {
  \$rc=0 if \$_ =~ /CRS-2676/;
 }
 elsif (\$action eq "stop")
 {
  \$rc=0 if \$_ =~ /CRS-2677/;
 }
 else {
  \$rc=0 if \$_ =~ /STATE=ONLINE/;
 }
}
close CRSTEST;

(\$date,\$time) = &timestamp();
print FILE "\$date \$time command \$action completed with rc \$rc\n";
print FILE  "\n" ;
close FILE;
exit \$rc;

sub timestamp {
  my (\$date,\$time);
  my (\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst) = localtime(time);

        \$year += 1900;
        \$mon++;
        \$date = sprintf("%4d-%2.2d-%2.2d",\$year,\$mon,\$mday);
        \$time = sprintf("%2.2d:%2.2d:%2.2d",\$hour,\$min,\$sec);
        return(\$date,\$time);
}

__EOF__
